// components/my-commodity/my-commodity.js
var common = require("../../utils/public.js") //引入public.js脚本
var api = require('../../utils/api.js') //api接口
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    
    pro_list: {
      type: Array,
      value: []
    },
    login: {
      type: Boolean
    },
    loginCode: {
      type: Boolean
    },
    loginCode1: {
      type: Boolean
    }
    
  },

  /**
   * 组件的初始数据
   */
  data: {
    pro_list:[],
  },
  /**
   * 组件的方法列表
   */
  methods: {
    
    // 跳转商品详情
    todetail: common.throttleFunc(function (e) {
      if (wx.getStorageSync('codeNum') == 103) {
        //未注册商铺 跳转商铺注册页
        wx.showToast({
          title: '您还未注册店铺！',
          icon: 'none',
          duration: 1500
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/type/type?type=' + 1,
          })

        }, 1000)
      } else if (wx.getStorageSync('codeNum') == 104) {
        //商铺审核中 
        wx.showToast({
          title: '您的商铺审核中！',
          icon: 'none',
          duration: 1000
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/shopBack/shopBack?type=' + 1,
          })

        }, 1000)

      }
      else if (wx.getStorageSync('codeNum') == 105) {
        //商铺审核未通过
        wx.showToast({
          title: '您申请的店铺暂未审核通过！',
          icon: 'none',
          duration: 1000
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/type/type?type=' + 1,
          })

        }, 1000)
      }
      else if (wx.getStorageSync('codeNum') == 99) {
        //登录跳转商品详情界面
        wx.navigateTo({
          url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id,
        })
      } else if (wx.getStorageSync("userMessage") == '') {
        wx.navigateTo({
          url: '/pages/userMess/userMess',
        })

      } else {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }
    }),

    showCar: common.throttleFunc(function (event) {
      let that = this
      that.setData({
        showCar: event
      })
    }),
    //点击隐藏购物车
    btnStop:function(e){

      if (wx.getStorageSync('codeNum') == 103) {
        //未注册商铺 跳转商铺注册页
        wx.showToast({
          title: '您还未注册店铺！',
          icon: 'none',
          duration: 1500
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/type/type?type=' + 1,
          })

        }, 1000)
      } else if (wx.getStorageSync('codeNum') == 104) {
        //商铺审核中 
        wx.showToast({
          title: '您的商铺审核中！',
          icon: 'none',
          duration: 1000
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/shopBack/shopBack?type=' + 1,
          })

        }, 1000)

      }
      else if (wx.getStorageSync('codeNum') == 105) {
        //商铺审核未通过
        wx.showToast({
          title: '您申请的店铺暂未审核通过！',
          icon: 'none',
          duration: 1000
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/type/type?type=' + 1,
          })

        }, 1000)
      }
      else if (wx.getStorageSync('codeNum') == 99) {
        //登录
        let that=this
        let index = e.currentTarget.dataset.index//数组下标
        let pro_list = that.data.pro_list//商品列表数组
        let showCar = pro_list[index].showCar//如果购物车内商品数量为0  点击隐藏
        let startNum = pro_list[index].goodsDetailsEntityList[0].startNum * 1//起送数量
        let amount = pro_list[index].goodsDetailsEntityList[0].amount;//购物车内商品数量
        // console.log(showCar)
        pro_list[index].showCar = true, //隐藏购物车
        amount = startNum //购物车商品数量加
        pro_list[index].goodsDetailsEntityList[0].amount=amount
        // 访问接口更新数据
        // TODO 获取userId/cityId
        let userInfoId = wx.getStorageSync("openId")//用户ID
        let cityId = wx.getStorageSync("cityId")//城市ID
        let goodsName = pro_list[index].name//商品名
        let goodsDetailsId = pro_list[index].goodsDetailsEntityList[0].id//商品id
        let totalPrice = pro_list[index].goodsDetailsEntityList[0].priceVip//vip价格
        let price = pro_list[index].goodsDetailsEntityList[0].price//商品价格
        //购物车接口
        api.goodsNumber({
          cityId,
          userInfoId,
          goodsName,
          goodsDetailsId,
          amount,
          price,
          totalPrice
        }).then(res => {
          if (res.result) {
            if (res.data > 0) {
              wx.setTabBarBadge({
                index: 3,
                text: res.data //显示的内容 
              })
            } else {
              wx.hideTabBarRedDot({
                index: 3
              })
            }
            // 重新渲染 ---显示新的数量
            that.setData({
              pro_list: pro_list//返回数组
            })
            that.triggerEvent("getTo", { pro_list })
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })
          }
        })
     
      } else if (wx.getStorageSync("userMessage")==''){
        wx.navigateTo({
          url: '/pages/userMess/userMess',
        })
      
      } else{
        wx.navigateTo({
          url: '/pages/login/login',
        })
      }
    },
    //购物车内商品数量加
    addNum: function (e) {
      let index = e.currentTarget.dataset.index//数组下标
      let pro_list = this.data.pro_list
      let amount = pro_list[index].goodsDetailsEntityList[0].amount;//购物车内商品数量
      let startNum = pro_list[index].goodsDetailsEntityList[0].startNum * 1//起送数量
      //购物车内商品数量加1
      amount=amount+1

      

      let that = this
      // 访问接口更新数据
      // TODO 获取userId/cityId
      let userInfoId = wx.getStorageSync("openId")//用户ID
      let cityId = wx.getStorageSync("cityId")//城市ID
      let goodsName = pro_list[index].name//商品名
      let goodsDetailsId = pro_list[index].goodsDetailsEntityList[0].id//商品id
      let totalPrice = pro_list[index].goodsDetailsEntityList[0].priceVip//vip价格
      let price = pro_list[index].goodsDetailsEntityList[0].price//商品价格
      //购物车接口
      api.goodsNumber({
        cityId,
        userInfoId,
        goodsName,
        goodsDetailsId,
        amount,
        price,
        totalPrice
      }).then(res => {
        if (res.result) {
          if (res.data > 0) {
            wx.setTabBarBadge({
              index: 3,
              text: res.data //显示的内容 
            })
          } else {
            wx.hideTabBarRedDot({
              index: 3
            })
          }

          pro_list[index].goodsDetailsEntityList[0].amount = amount

          // 重新渲染 ---显示新的数量
          that.setData({
            pro_list: pro_list//返回数组
          })
          that.triggerEvent("getTo", { pro_list })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    },
    //购物车内商品数量减
    subNum: function (e) {
      let index = e.currentTarget.dataset.index//数组下标
      let pro_list = this.data.pro_list
      let amount = pro_list[index].goodsDetailsEntityList[0].amount;//购物车内商品数量
      let startNum = pro_list[index].goodsDetailsEntityList[0].startNum * 1//起送数量
      //购物车内商品数量减
      if (amount == startNum) {
        amount = 0
        // 购物车内商品数量 如果为0 隐藏加减 显示购物车
        pro_list[index].showCar = !pro_list[index].showCar
      } else {
        amount = amount - 1;
      }
      pro_list[index].goodsDetailsEntityList[0].amount=amount
      this.setData({
        pro_list: pro_list,
      })
      this.triggerEvent("getTo", { pro_list })
      let that = this
      // 访问接口更新数据
      // TODO 获取userId/cityId
      let userInfoId = wx.getStorageSync("openId")//用户ID
      let cityId = wx.getStorageSync("cityId")//城市ID
      let goodsName = pro_list[index].name//商品名
      let goodsDetailsId = pro_list[index].goodsDetailsEntityList[0].id//商品id
      let totalPrice = pro_list[index].goodsDetailsEntityList[0].priceVip//vip价格
      let price = pro_list[index].goodsDetailsEntityList[0].price//商品价格
      //购物车接口
      api.goodsNumber({
        cityId,
        userInfoId,
        goodsName,
        goodsDetailsId,
        amount,
        price,
        totalPrice
      }).then(res => {
        console.log(res)
        if (res.data > 0) {
          wx.setTabBarBadge({
            index: 3,
            text: res.data //显示的内容 
          })
        }else{
          wx.hideTabBarRedDot({
            index: 3
          })
        }
      })
    },
    
    
  },
})
